package gui.admin;

import database.TableDatabaseUsage;
import gui.ATM;
import info.Admin;
import info.Flight;
import messages.admin.UpAdmInfoReqMes;
import messages.admin.UpAdmInfoResponeMessage;
import server.MessageServer;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.io.IOException;
import java.io.Serializable;

public class ManageAdmin extends JFrame implements Serializable {
    JFrame jf =new JFrame();
    private String MangerType;
    private int views;
    public Flight flight;
    private JTable jTable;

//    FlightId flightId;
    private String flightsId;
    private String flightsArea;

    public ManageAdmin(Admin admin) {
        views=1;

        jTable = new JTable(ATM.getInstance());

        jf.setLayout(null);
        //设置三个Panel分块
        JPanel jp1 = new JPanel(new GridLayout(5,1));
        jp1.setBounds(0,0,200,620);

        jp1.setLayout(null);
        JPanel jp2 = new JPanel();
        jp2.setBounds(200,0,900,55);
        JPanel jp3 = new JPanel();
        jp3.setBounds(200,55,900,620);



        //右侧菜单
        JLabel jLabel = new JLabel("欢迎");
        JMenu jm1=new JMenu(admin.getName()+"管理员");  //创建JMenu菜单对象
        JMenuItem t3 = new JMenuItem("个人信息");
        JMenuItem t4 = new JMenuItem("退出登录");
        jm1.add(t3);
        jm1.add(t4);
        JMenuBar br1 = new JMenuBar();
        br1.setBounds(950,5,120,40);
        br1.add(jLabel);
        br1.add(jm1);
        br1.setBackground(Color.cyan);
        jf.add(br1);
        t3.addActionListener(e->{
            new AdminInfoForm(admin);
        });

        //添加按钮
        JButton btn1 = new JButton("主    页");
        btn1.setBounds(10,10,180,60);

        JButton btn2 = new JButton("航班管理");
        btn2.setBounds(10,80,180,60);
        btn2.addActionListener(e -> {
            new ManageFlights();
        });

        JButton btn3 = new JButton("票务管理");
        btn3.setBounds(10,150,180,60);
        btn3.addActionListener(e->{
            new ManageTickets().start();
        });


        JButton btn4 = new JButton("用户信息管理");
        btn4.setBounds(10,220,180,60);
        btn4.addActionListener(e->{
            new ManageUsers();

        });
        JButton btn5 = new JButton("员工信息管理");
        btn5.setBounds(10,290,180,60);
        btn5.addActionListener(e->{
            new ManageStaff();
        });

        jp1.add(btn1);
        jp1.add(btn2);
        jp1.add(btn3);
        jp1.add(btn4);
        jp1.add(btn5);

        //搜索栏
        // 创建一个文本字段用于输入搜索词
        // 创建一个标签用于描述搜索类型
        JLabel typeLabel = new JLabel("搜索范围:");
        typeLabel.setBounds(55,10,100,40);
        jp2.add(typeLabel);
        jp2.setLayout(null);
        // 创建一个下拉菜单用于选择搜索类型
        MangerType="航班号";
        String[] searchTypes = {"航班号", "地点", "日期"};
        JComboBox<String> searchTypeComboBox = new JComboBox<>(searchTypes);
        searchTypeComboBox.addActionListener(e->{
            switch (searchTypeComboBox.getSelectedIndex()){
                case 1:
                    MangerType="地点";
                    System.out.println("您使用地点查询！");
                    break;
                case 2:
                    MangerType="日期";
                    System.out.println("您使用日期查询！");
                    break;
                default:
                    MangerType="航班号";
                    System.out.println("您使用航班号查询！");
                    break;
            }
            if (MangerType=="航班号"){
                views=1;
            } else if (MangerType=="地点") {
                views=2;
            }

        });


        searchTypeComboBox.setBounds(150,10,120,40);

        jp2.add(searchTypeComboBox);
        JTextField searchField = new JTextField(20);
        searchField.setBounds(300,10,300,40);
        jp2.add(searchField);

//        searchField.addActionListener(e->{
////            if(searchField.getText()!=""){
////                flightId.setFlightId(searchField.getText());
////                System.out.println(flightId);
////            }
//        });
        // 创建一个按钮用于触发搜索操作
        JButton searchButton = new JButton("搜索");
        searchButton.setBounds(610,10,100,40);
        jp2.add(searchButton);

        // 为按钮添加一个动作监听器
        searchButton.addActionListener(e -> {
            // 获取用户输入的搜索词
            String searchTerm = searchField.getText();
            // 在这里执行搜索操作（例如，查询数据库或过滤数据）
            // 这里我们只是简单地将搜索词显示在标签上作为示例
            System.out.println("你搜索的是："+searchTerm);
//            flightId.setFlightId(searchTerm);


            if(views==1 && !searchTerm.equals("")){
                flightsId=searchTerm;
//                jTable.setModel(null);
                ATM.getInstance().flightList = TableDatabaseUsage.getInstance().getFlightList1(flightsId);


//                ATM1.getInstance().setFlightId(searchTerm);
//               flightId.setFlightId(ATM1.getInstance().setFlightId(searchTerm));
//                FlightId.setFlightId(searchTerm);
//                start();
                searchField.setText("");
                jTable.updateUI();
//                new Pannel().ManageAdmin1(abstractTableModel);
            } else if (views==2 &&!searchTerm.equals("")) {
                jTable.updateUI();
                flightsArea=searchTerm;
                ATM.getInstance().flightList=TableDatabaseUsage.getInstance().getFlightList2(flightsArea);
                searchField.setText("");
                jTable.updateUI();
            }


        });
//        setFlightId(flightId);

        JScrollPane scroll = new JScrollPane(jTable);

        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setPreferredSize(new Dimension(800, 300));
        jTable.setBackground(Color.white);
        jTable.setRowHeight(40);

        jTable.setGridColor(Color.pink);
        jTable.setSelectionBackground(Color.orange);
        //使表格文字居中
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();

        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        jp3.add(scroll);

        jf.setTitle("机票预定管理系统");
        jf.add(jp1);
        jf.add(jp2);
        jf.add(jp3);
        jf.setBounds(400,200,1100,400);
        jf.setVisible(true);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



//    public ManageAdmin() {
//
//    }
    class Pannel extends JFrame{
        public void ManageAdmin1(AbstractTableModel abstractTableModel){
            setLocation(200,55);
            setSize(900, 620);
            JPanel jp3 = new JPanel();
            jp3.setBounds(200,55,900,620);
            JTable jTable = new JTable(abstractTableModel);
            JScrollPane scroll = new JScrollPane(jTable);

            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            scroll.setPreferredSize(new Dimension(800, 300));
            jTable.setBackground(Color.white);
            jTable.setRowHeight(40);

            jTable.setGridColor(Color.pink);
            jTable.setSelectionBackground(Color.orange);
            //使表格文字居中
            DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();

            centerRenderer.setHorizontalAlignment(JLabel.CENTER);
            jp3.add(scroll);
            add(jp3);
            setVisible(true);
        }
}


    private JTextField nameInfoTextField;
    private JTextField identityNumInfoTextField;
    private JTextField phoneInfoTextField;
    private Admin cacheAdmin= new Admin();

    public ManageAdmin() {

    }

    class AdminInfoForm extends JFrame {
        public AdminInfoForm(Admin admin) {
            super("个人信息");
            setSize(500, 500);
            setLayout(new GridLayout(4, 2));

            cacheAdmin = admin;

            nameInfoTextField = new JTextField(cacheAdmin.getName());
            nameInfoTextField.setEditable(false);

            identityNumInfoTextField = new JTextField(cacheAdmin.getIdno());
            identityNumInfoTextField.setEditable(false);

            phoneInfoTextField = new JTextField(cacheAdmin.getTelephone());
            phoneInfoTextField.setEditable(false);

            JButton updateInfoButton = new JButton("修改信息");
            updateInfoButton.addActionListener(e -> {
                new UpdateAdminInfoForm(cacheAdmin);
            });

            JLabel l1 = new JLabel("姓名：");
            JLabel l2 = new JLabel("身份证：");
            JLabel l3 = new JLabel("电话：");

            // 设置居中
            l1.setHorizontalAlignment(JLabel.CENTER);
            l2.setHorizontalAlignment(JLabel.CENTER);
            l3.setHorizontalAlignment(JLabel.CENTER);

            add(l1);
            add(nameInfoTextField);
            add(l2);
            add(identityNumInfoTextField);
            add(l3);
            add(phoneInfoTextField);
            add(new JLabel(""));
            add(updateInfoButton);

            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setVisible(true);
        }
    }

    class UpdateAdminInfoForm extends JFrame {
        JTextField nameTextField;
        JTextField identityNumTextField;
        JTextField phoneTextField;
        private String oldIDNum;
        private Admin newAdmin= new Admin();

        public UpdateAdminInfoForm(Admin admin) {
            super("修改个人信息");
            setSize(500, 500);
            setLayout(new GridLayout(4, 2));
            oldIDNum = admin.getIdno();
            nameTextField = new JTextField(admin.getName());
            identityNumTextField = new JTextField(admin.getIdno());
            phoneTextField = new JTextField(admin.getTelephone());
            JButton updateInfoButton = new JButton("提交修改");
            updateInfoButton.addActionListener(e -> {
                newAdmin.setIdno(identityNumInfoTextField.getText());
                newAdmin.setName(nameTextField.getText());
                newAdmin.setTelephone(phoneTextField.getText());
                sendUpdateAdminInfoMsg(newAdmin,oldIDNum);
                dispose();
            });

            JLabel l1 = new JLabel("姓名：");
            JLabel l2 = new JLabel("证件号：");
            JLabel l3 = new JLabel("电话：");

            // 设置居中
            l1.setHorizontalAlignment(JLabel.CENTER);
            l2.setHorizontalAlignment(JLabel.CENTER);
            l3.setHorizontalAlignment(JLabel.CENTER);

            add(l1);
            add(nameTextField);
            add(l2);
            add(identityNumTextField);
            add(l3);
            add(phoneTextField);

            add(new JLabel(""));
            add(updateInfoButton);

            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setVisible(true);


        }
        private boolean sendUpdateAdminInfoMsg(Admin admin, String oldIDNum) {
            UpAdmInfoReqMes up_req_msg = new UpAdmInfoReqMes(admin);
            UpAdmInfoResponeMessage up_ack_msg;
            up_req_msg.setAdmin(admin);
            up_req_msg.setOldIDNum(oldIDNum);

            try {
                MessageServer.getInstance().getOos().writeObject(up_req_msg);
                System.out.println("客户端发送请求成功！" + up_req_msg);
                up_ack_msg = (UpAdmInfoResponeMessage) MessageServer.getInstance().getOis().readObject();
                System.out.println("客户端收到服务端的响应成功！" + up_ack_msg);
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }

            updateInfo(up_ack_msg.getNewadmin());
            return true;
        }

        private void updateInfo(Admin newAdmin) {
            cacheAdmin.setName(newAdmin.getName());
            cacheAdmin.setIdno(newAdmin.getIdno());
            cacheAdmin.setTelephone(newAdmin.getTelephone());
            nameInfoTextField.setText(newAdmin.getName());
            identityNumInfoTextField.setText(newAdmin.getIdno());
            phoneInfoTextField.setText(newAdmin.getTelephone());
        }

}

}
